home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
10,000 Great Games
/
10,000 Great Games.iso
/
Product
/
66
/
data1.cab
/
Source_Files
/
Src
/
Props.cpp
< prev
next >
Wrap
C/C++ Source or Header
|
2000-01-16
|
10KB
|
600 lines
#include "stdafx.h"
#include <string.h>
#include <ctype.h>
cProperties *props = 0;
static cProperties *find = 0;
static char *find_name = 0, *find_type = 0;
static cParse *pars;
static cProperties *cp;
static char *cl;
cProperties::cProperties()
{
add_end((cList **)&props);
// Set name and type
type = "";
objtype = 0;
name = "";
// Reset images
images = 0;
// Reset sounds
sounds = 0;
// Reset boundaries
line = 0;
box = 0;
spot = 0;
circle = 0;
// Reset random level options
placement = PLACEMENT_NEVER;
no_overlap = TRUE;
occurence = 1000;
link_to = 0;
// Reset cmap
cmap = 0;
// Reset extra parameters
params = 0;
}
cProperties::~cProperties()
{
// Delete images
images->delete_list();
// Delete sounds
sounds->delete_list();
// Delete linkto's
link_to->delete_list();
// Delete boundaries
line->delete_list();
box->delete_list();
spot->delete_list();
circle->delete_list();
// Delete extra parameters
params->delete_list();
}
void cProperties::go_next()
{
pars = (cParse *)pars->next;
}
void cProperties::add_sound()
{
// Add sound
cSound *s = new cSound(&(cp->sounds));
s->sample = cWAV::get(pars->value);
s->label = cl;
// Get parameters
go_next();
while (pars != 0)
{
if (pars->cmp("LOOP"))
{
s->loop = pars->value_true();
go_next();
}
else if (pars->cmp("DURATION"))
{
s->duration = (int)(atof(pars->value) * sec);
go_next();
}
else if (pars->cmp("VOLUME"))
{
s->volume = atoi(pars->value);
go_next();
}
// Otherwise go back
else
break;
}
}
void cProperties::add_image()
{
// Add image
cImage *i = new cImage(&(cp->images));
i->bmp = cBMP::get(pars->value);
i->label = cl;
i->origin_x = i->bmp->w / 2;
i->origin_y = i->bmp->h / 2;
// Get parameters
go_next();
while (pars != 0)
{
if (pars->cmp("DELAY"))
{
i->delay = (int)(atof(pars->value)*sec);
go_next();
}
else if (pars->cmp("COLORMAP"))
{
i->cmap = (char *)cData::get(pars->value);
go_next();
}
else if (pars->cmp("GRAVITY"))
{
i->gmap = (short *)cData::get(pars->value);
go_next();
}
else if (pars->cmp("ORIGIN"))
{
char x, y;
if (sscanf(pars->value, "%c%d,%c%d", &x, &(i->origin_x), &y, &(i->origin_y)) != 4)
pars->invalid_line();
switch(toupper(x))
{
case 'L':
break;
case 'C':
case 'M':
i->origin_x += i->bmp->w / 2;
break;
case 'R':
i->origin_x += i->bmp->w - 1;
break;
default:
pars->invalid_line();
}
switch(toupper(y))
{
case 'T':
break;
case 'C':
case 'M':
i->origin_y += i->bmp->h / 2;
break;
case 'B':
i->origin_y += i->bmp->h - 1;
break;
default:
pars->invalid_line();
}
go_next();
}
// Otherwise go back
else
break;
}
}
void cProperties::add_linkto()
{
int x, y;
// Create linkto object
cLinkTo *l = new cLinkTo(&cp->link_to);
// Store occurence
l->chance = atoi(pars->value);
go_next();
// Set other properties
while (pars != 0)
{
if (pars->cmp("TYPE"))
{
l->type = pars->value;
go_next();
}
else if (pars->cmp("NAME"))
{
l->name = pars->value;
go_next();
}
else if (pars->cmp("SPOT"))
{
if (sscanf(pars->value, "%d,%d", &x, &y) != 2)
pars->invalid_line();
l->position.x = x, l->position.y = y;
go_next();
}
else
break;
}
}
void cProperties::add_type()
{
int x1, y1, x2, y2;
// New property object
cp = new cProperties ();
cp->type = pars->value;
cp->objtype = cObjectTypes::find(pars->value);
// Reset label
cl = "";
// Get parameters
go_next();
while (pars != 0)
{
if (pars->cmp("NAME"))
{
cp->name = pars->value;
go_next();
}
else if (pars->cmp("LINE"))
{
if (sscanf(pars->value, "%d,%d,%d,%d", &x1, &y1, &x2, &y2) != 4)
pars->invalid_line();
new cLine (&(cp->line), x1, y1, x2, y2, cl);
go_next();
}
else if (pars->cmp("BOX"))
{
if (sscanf(pars->value, "%d,%d,%d,%d", &x1, &y1, &x2, &y2) != 4 || x1 > x2 || y1 > y2)
pars->invalid_line();
new cBox (&(cp->box), x1, y1, x2, y2, cl);
go_next();
}
else if (pars->cmp("SPOT"))
{
if (sscanf(pars->value, "%d,%d", &x1, &y1) != 2)
pars->invalid_line();
new cSpot (&(cp->spot), x1, y1, cl);
go_next();
}
else if (pars->cmp("RADIUS"))
{
if (sscanf(pars->value, "%d", &x2) != 1)
pars->invalid_line();
new cCircle(&(cp->circle), 0, 0, x2, cl);
go_next();
}
else if (pars->cmp("CIRCLE"))
{
if (sscanf(pars->value, "%d,%d,%d", &x1, &y1, &x2) != 3)
pars->invalid_line();
new cCircle(&(cp->circle), x1, y1, x2, cl);
go_next();
}
else if (pars->cmp("PLACEMENT"))
{
if (eq(pars->value, "NEVER"))
cp->placement = PLACEMENT_NEVER;
else if (eq(pars->value, "PLATFORM"))
cp->placement = PLACEMENT_PLATFORM;
else if (eq(pars->value, "ANY"))
cp->placement = PLACEMENT_ANY;
else if (eq(pars->value, "ON PLATFORM"))
cp->placement = PLACEMENT_ON_PLATFORM;
else if (eq(pars->value, "BETWEEN PLATFORMS"))
cp->placement = PLACEMENT_BETWEEN_PLATFORMS;
else if (eq(pars->value, "UNDER PLATFORM"))
cp->placement = PLACEMENT_UNDER_PLATFORM;
else
pars->invalid_line();
go_next();
}
else if (pars->cmp("NO_OVERLAP"))
{
cp->no_overlap = pars->value_true();
go_next();
}
else if (pars->cmp("BOUNDING_BOX"))
{
if (sscanf(pars->value, "%d,%d,%d,%d", &cp->bbox.x1, &cp->bbox.y1, &cp->bbox.x2, &cp->bbox.y2) != 4
|| cp->bbox.x1 > cp->bbox.x2
|| cp->bbox.y1 > cp->bbox.y2)
pars->invalid_line();
go_next();
}
else if (pars->cmp("LINKTO"))
add_linkto();
else if (pars->cmp("OCCURENCE"))
{
cp->occurence = atoi(pars->value);
go_next();
}
else if (pars->cmp("COLORMAP"))
{
cp->cmap = (char *)cData::get(pars->value);
go_next();
}
else if (pars->cmp("LABEL") || pars->cmp("SEQUENCE") || pars->cmp("SOUNDSEQUENCE"))
{
cl = pars->value;
go_next();
}
else if (pars->label[0] == '*')
{
new cParse (&(cp->params), pars);
go_next();
}
else if (pars->cmp("IMAGE"))
add_image();
else if (pars->cmp("SOUND"))
add_sound();
// Otherwise go back
else
break;
}
}
void cProperties::create_all(cParse *p)
{
pars = p;
while (pars != 0)
{
// Check if new object has to be made
if (pars->cmp("TYPE"))
add_type();
// Otherwise there's an error
else
pars->invalid_line();
}
}
cProperties *cProperties::find_first(char *type, char *name)
{
find = props, find_name = name, find_type = type;
return find_next();
}
cProperties *cProperties::find_next()
{
for (; find != 0; find = (cProperties *)find->next)
if ((find_name == 0 || eq(find->name, find_name))
&& (find_type == 0 || eq(find->type, find_type)))
{
cProperties *found = find;
find = (cProperties *)find->next;
return found;
}
return 0;
}
cProperties *cProperties::find_w_error(char *type, char *name)
{
cProperties *p = cProperties::find_first(type, name);
if (p == 0)
error("Unable to find object \"%s\"/\"%s\"", type == 0? "ALL":type, name == 0? "ALL":name);
return p;
}
int cProperties::sequence_exists(char *_name)
{
ASSERT(_name != 0);
for (cImage *i = images; i != 0 && !eq(_name, i->label); i = (cImage *)i->next);
return i != 0;
}
void cProperties::get_sequence(char *_name, cAnimation &anim)
{
// Check if all images are requested
if (_name == 0)
{
anim.start_frame = images,
anim.end_frame = (cImage *)cList::get_end(images);
return;
}
// Search for name
cImage *i;
for (i = images; i != 0 && !eq(_name, i->label); i = (cImage *)i->next);
// Check if there is a sequence
if (i == 0)
{
anim.start_frame = 0;
anim.end_frame = 0;
return;
}
// Else store first picture
anim.start_frame = i;
// Get end of animation
for (; i->next != 0 && eq(_name, ((cImage *)i->next)->label); i = (cImage *)i->next);
anim.end_frame = i;
}
int cProperties::soundsequence_exists(char *_name)
{
ASSERT(_name != 0);
for (cSound *s = sounds; s != 0 && !eq(_name, s->label); s = (cSound *)s->next);
return s != 0;
}
void cProperties::get_soundsequence(char *_name, cSoundSequence &seq)
{
// Check if all sounds are requested
if (_name == 0)
{
seq.start_sound = sounds;
seq.end_sound = (cSound *)cList::get_end(sounds);
return;
}
// Search for name
cSound *s;
for (s = sounds; s != 0 && !eq(_name, s->label); s = (cSound *)s->next);
// Check if sequence exists
if (s == 0)
{
seq.start_sound = 0;
seq.end_sound = 0;
return;
}
// Else store first picture
seq.start_sound = s;
// Get end of sequence
for (; s->next != 0 && eq(_name, ((cSound *)s->next)->label); s = (cSound *)s->next);
seq.end_sound = s;
}
void cProperties::get_circles(char *_name, cCircle **list)
{
ASSERT(_name != 0);
*list = 0;
for (cCircle *c = circle; c != 0; c = (cCircle *)c->next)
if (eq(c->label, _name))
new cCircle(list, c);
ASSERT(*list != 0);
}
void cProperties::get_spots(char *_name, cSpot **list)
{
ASSERT(_name != 0);
*list = 0;
for (cSpot *s = spot; s != 0; s = (cSpot *)s->next)
if (eq(s->label, _name))
new cSpot(list, s);
ASSERT(*list != 0);
}